package com.example.x5webviewdemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.x5webviewdemo.activity.BrowserActivity;
import com.example.x5webviewdemo.activity.CustomWebViewActivity;
import com.example.x5webviewdemo.activity.FullScreenPlayerActivity;
import com.example.x5webviewdemo.activity.LoginActivity;
import com.example.x5webviewdemo.activity.X5WebViewActivity;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * @作者: njb
 * @时间: 2019/11/8 10:32
 * @描述: 主界面
 */
public class MainActivity extends AppCompatActivity {
    private static String[] titles = null;
    private static final int TBS_WEB = 0;
    private static final int FULL_SCREEN_VIDEO = 1;
    private static final int FILE_CHOOSER = 2;
    private static final int CUSTOM_WEBVIEW = 3;
    private Context mContext = null;
    private SimpleAdapter gridAdapter;
    private GridView gridView;
    private ArrayList<HashMap<String, Object>> items;

    private static boolean main_initialized = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_advanced);
        mContext = this;
        if (!main_initialized) {
            this.new_init();
        }
    }

    @Override
    protected void onResume() {
        this.new_init();
        super.onResume();
    }

    private void new_init() {
        items = new ArrayList<>();
        this.gridView = this.findViewById(R.id.item_grid);

        if (gridView == null) {
            throw new IllegalArgumentException("the gridView is null");
        }

        titles = getResources().getStringArray(R.array.index_titles);
        int[] iconResourse = { R.drawable.tbsweb, R.drawable.fullscreen,
                R.drawable.filechooser,R.drawable.tbsweb };

        HashMap<String, Object> item;
        for (int i = 0; i < titles.length; i++) {
            item = new HashMap<>();
            item.put("title", titles[i]);
            item.put("icon", iconResourse[i]);

            items.add(item);
        }
        this.gridAdapter = new SimpleAdapter(this, items,
                R.layout.function_block, new String[] { "title", "icon" },
                new int[] { R.id.Item_text, R.id.Item_bt });
        if (null != this.gridView) {
            this.gridView.setAdapter(gridAdapter);
            this.gridAdapter.notifyDataSetChanged();
            this.gridView.setOnItemClickListener((gridView, view, position, id) -> {
                Intent intent;
                switch (position) {
                    case FILE_CHOOSER: {
                        intent = new Intent(MainActivity.this,
                                X5WebViewActivity.class);
                        MainActivity.this.startActivity(intent);

                    }
                    break;
                    case FULL_SCREEN_VIDEO: {
                        intent = new Intent(MainActivity.this,
                                FullScreenPlayerActivity.class);
                        MainActivity.this.startActivity(intent);
                    }
                    break;

                    case TBS_WEB: {
                        intent = new Intent(MainActivity.this,
                                LoginActivity.class);
                        MainActivity.this.startActivity(intent);

                    }
                    break;
                    case CUSTOM_WEBVIEW:
                        intent = new Intent(MainActivity.this,
                                CustomWebViewActivity.class);
                        MainActivity.this.startActivity(intent);
                        break;
                }
            });

        }
        main_initialized = true;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                this.tbsSuiteExit();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void tbsSuiteExit() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle("X5功能演示");
        dialog.setPositiveButton("OK", (dialog1, which) -> {
            Process.killProcess(Process.myPid());
            dialog1.dismiss();
        });
        dialog.setMessage("quit now?");
        dialog.create().show();
    }
}
